package com.android.lovingpet.signup.view;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;


import com.android.lovingpet.R;
import com.android.lovingpet.base.TakePhotoFragment;
import com.android.lovingpet.home.view.HomeActivity;
import com.android.lovingpet.signup.ILoginContract;
import com.android.lovingpet.signup.LoginInPresenter;
import com.android.lovingpet.util.Utility;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.jph.takephoto.model.TResult;

import java.io.File;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by ikata on 2018/1/23.
 */
public class LoginInMasterDataFragment extends TakePhotoFragment<ILoginContract.View,LoginInPresenter> implements View.OnClickListener, ILoginContract.View {

    private EditText et_name;
    private CircleImageView headImage;
    private ImageView im_woman;
    private ImageView im_man;
    private File image;
    private PopupWindow popupWindow;
    private int sex;

    @Override
    protected int setContentView() {
        return R.layout.fragment_login_in_masterdata;
    }

    @Override
    protected void onCreateView(View view) {
        image = new File(getContext().getFilesDir() + File.separator + "defaultFace.jpg");
        ((TextView) view.findViewById(R.id.tv_title)).setText("主人资料");
        Button bt_commit = view.findViewById(R.id.bt_commit);
        headImage = view.findViewById(R.id.headImage);
        im_man = view.findViewById(R.id.ic_man);
        im_woman = view.findViewById(R.id.ic_woman);
        et_name = view.findViewById(R.id.masterName);
        im_man.setOnClickListener(this);
        im_woman.setOnClickListener(this);
        bt_commit.setOnClickListener(this);
        headImage.setOnClickListener(this);
        setGender(1);
    }

    @Override
    public void onResume() {
        super.onResume();
        reBind();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_commit:
                getPresenter().commitInfo(image, et_name.getText().toString(), sex);
                break;
            case R.id.headImage:
                popUpwindow(getView());
                break;
            case R.id.ic_man:
                setGender(1);
                break;
            case R.id.ic_woman:
                setGender(0);
                break;
            default:
                break;
        }
    }

    @Override
    public LoginInPresenter createPresenter() {
        //return new LoginInPresenter();
        return ((LoginInActivity) getActivity()).getPresenter();
    }

    @Override
    public ILoginContract.View createView() {
        return this;
    }

    @Override
    public void next() {
        Intent intent = new Intent(getActivity(), HomeActivity.class);
        startActivity(intent);
        Utility.exitActivity(getActivity());
    }

    @Override
    public void takeSuccess(TResult result) {
        super.takeSuccess(result);
        image = new File(result.getImage().getOriginalPath());
        Glide.with(this)
                .load(image)
                .skipMemoryCache(true)
                .diskCacheStrategy(DiskCacheStrategy.RESULT)
                .into(headImage);
    }

    @Override
    public void takeFail(TResult result, String msg) {
        super.takeFail(result, msg);
        Toast.makeText(getActivity(), "Error:" + msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void takeCancel() {
        super.takeCancel();
    }

    private void setGender(int sex) {
        this.sex = sex;
        if (sex == 1) {
            im_man.setImageDrawable(getContext().getResources().getDrawable(R.drawable.ic_man));
            im_woman.setImageDrawable(getContext().getResources().getDrawable(R.drawable.ic_woman1));
        } else {
            im_man.setImageDrawable(getContext().getResources().getDrawable(R.drawable.ic_man1));
            im_woman.setImageDrawable(getContext().getResources().getDrawable(R.drawable.ic_woman));
        }
    }

    //PopupWindow
    public void popUpwindow(View view) {
        LinearLayout layout = (LinearLayout) getLayoutInflater().inflate(R.layout.my_photo_window, null);
        if (popupWindow != null && popupWindow.isShowing()) {
            return;
        }
        popupWindow = new PopupWindow(layout,
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        //点击空白处时，隐藏掉pop窗口
        popupWindow.setFocusable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        //添加弹出、弹入的动画
        popupWindow.setAnimationStyle(R.style.Popupwindow);
        int[] location = new int[2];
        view.getLocationOnScreen(location);
        popupWindow.showAtLocation(view, Gravity.LEFT | Gravity.BOTTOM, 0, -location[1]);
        //添加按键事件监听
        setButtonListeners(layout);
    }

    public void setButtonListeners(LinearLayout layout) {
        Button camera =  layout.findViewById(R.id.camera);
        Button gallery = layout.findViewById(R.id.gallery);
        Button cancel =  layout.findViewById(R.id.cancel);
        camera.setOnClickListener(view -> {
            //相机
            if (popupWindow != null && popupWindow.isShowing()) {
                imageUri = getImageCropUri();
                //拍照并裁剪
                takePhoto.onPickFromCaptureWithCrop(imageUri, cropOptions);
                //仅仅拍照不裁剪
                //takePhoto.onPickFromCapture(imageUri);
            }
            getActivity().overridePendingTransition(R.anim.window_back, R.anim.window_out);
            popupWindow.dismiss();

        });
        //相册
        gallery.setOnClickListener(view -> {
            if (Utility.hasSdcard() == true && popupWindow != null && popupWindow.isShowing()) {
                imageUri = getImageCropUri();
                //从相册中选取图片并裁剪
                takePhoto.onPickFromGalleryWithCrop(imageUri, cropOptions);
                //从相册中选取不裁剪
                //takePhoto.onPickFromGallery();
                popupWindow.dismiss();
            }
        });
        cancel.setOnClickListener(view -> {
            if (popupWindow != null && popupWindow.isShowing()) {
                popupWindow.dismiss();
            }
        });
    }
}


